<?php
/**
 * CommentController.php
 *
 * @category    Madoqua
 * @package     Module
 * @subpackage  BlogAdmin
 */

/** base class */
require_once 'Madoqua/Controller/Action/Abstract.php';

/**
 * BlogAdmin_CommentController
 *
 * @category    Madoqua
 * @package     Module
 * @subpackage  BlogAdmin
 */
class BlogAdmin_CommentController extends Madoqua_Controller_Action_Abstract
{
    /**
     * Post model
     *
     * @var Blog_Post
     */
    private $_postModel;

    /**
     * Comments model
     *
     * @var Blog_Comment
     */
    private $_commentModel;

    /**
     * initialization
     */
    public function init() {
        parent::init();

        $this->_modelLoader->load(array('Blog_Post', 'Blog_Comment'));
        $this->_postModel = new Blog_Post();
        $this->_commentModel = new Blog_Comment();
        //set up model
        
        $ajaxContext = $this->_helper->getHelper('AjaxContext');
        $ajah = array('table');
        foreach($ajah as $action) {
            $ajaxContext->addActionContext($action, 'html');
        }
        $ajaxContext->initContext();
        //set up the content helper for certain actions        
    }

    /**
     * index
     */
    public function indexAction()
    {
    }
    
    /**
     * read a single comment
     */
    public function readAction() {
        $id = (int) $this->_getParam('id', 0);
        $comment = $this->_commentModel->find($id)->current();
        if ($comment == null) {
            throw new Exception('Comment not found');        
        }
        
        $this->view->comment = $comment;
        $this->view->post = $comment->findParentRow('Blog_Post');
    }

    /**
     * recent comments
     */
    public function recentAction()
    {
        $count = (int) $this->_getParam('count', 5);
        $comments = $this->_commentModel->fetchList(
            $this->_commentModel->getAdapter()
                                ->select()                                
                                ->where('spam = 0')
                                ->order('c.accepted ASC')
                                ->order('c.posted DESC'), null, $count);
        //select $count accepted comments sorted by id desc
        
        $this->view->comments = $comments;
    }
    
    /**
     * a nice table of comments
     */
    public function tableAction()
    {
        $count = (int) $this->_getParam('count', 10);
        //count
        
        $sort = strtolower(trim($this->getRequest()->getParam('sort', 'posted')));
        if (!in_array($sort, $this->_commentModel->info('cols'))) {
            throw new Exception($sort . ' is not a column');
        }
        //column
        
        $dir = strtoupper(trim($this->getRequest()->getParam('dir', 'DESC')));
        if (!in_array($dir, array('DESC', 'ASC'))) {
            throw new Exception($dir . ' is not a valid sort direction');
        }
        //sort dir
        
        $comments = $this->_commentModel->fetchList(null, $sort . ' ' . $dir, $count);
        $this->view->comments = $comments;
        $this->view->sort = $sort;
        $this->view->dir = strtolower($dir);        
    }    
    
    /**
     * recent spam
     */
    public function recentSpamAction() {
        $count = (int) $this->_getParam('count', 5);
        $comments = $this->_commentModel->fetchList(
            $this->_commentModel->getAdapter()->select()->where('accepted = 0')
            ->where('spam = 1')
        , null, $count);
        //select $count accepted comments sorted by id desc
        
        $this->view->comments = $comments;
        
        $this->render('recent');
        //re-use the recent comment view script
    }
    
    /**
     * Accept a comment and mark it as not-spam
     */
	public function acceptAction() {
		$id = (int)$this->_getParam('id');
		if(!$id) {
		    throw new Exception('id must be specified');
		}
		
		$this->_commentModel->accept($id);
		$this->_commentModel->markAsHam($id);
		
        $this->_helper->viewRenderer->setNoRender(true);
        $this->_helper->layout()->disableLayout();
        
        $redirector = $this->_helper->getHelper('Redirector');
        $redirector->setGoToSimple('index', 'comment', 'admin');
	}
	
    /**
     * Reject a comment and mark it as spam
     */
	public function rejectAction() {
		$id = (int)$this->_getParam('id');
		if(!$id) {
		    throw new Exception('id must be specified');
		}
		
		$this->_commentModel->reject($id);
		$this->_commentModel->markAsSpam($id);
		
        $this->_helper->viewRenderer->setNoRender(true);
        $this->_helper->layout()->disableLayout();
        
        $redirector = $this->_helper->getHelper('Redirector');
        $redirector->setGoToSimple('index', 'comment', 'admin');
	}
}
